import 'package:flutter/widgets.dart';
import 'package:mobile_app.core/core/presentation/widget_config.dart';

class PressableTile extends StatefulWidget {
  const PressableTile({
    required this.color,
    required this.pressedColor,
    required this.onPressed,
    required this.child,
    super.key,
  });

  final Color color;
  final Color pressedColor;
  final VoidCallback? onPressed;
  final Widget child;

  @override
  State<PressableTile> createState() => _PressableTileState();
}

class _PressableTileState extends State<PressableTile> {
  var _pressed = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTapDown: (tapDownDetails) {
        if (widget.onPressed != null) {
          setState(() {
            _pressed = true;
          });
        }
      },
      onTapCancel: () {
        if (widget.onPressed != null) {
          setState(() {
            _pressed = false;
          });
        }
      },
      onTap: () {
        if (widget.onPressed != null) {
          widget.onPressed!();
          Future.delayed(
            kTilePressEffectDuration,
            () {
              if (mounted) {
                setState(() {
                  _pressed = false;
                });
              }
            },
          );
        }
      },
      child: Container(
        color: _pressed ? widget.pressedColor : widget.color,
        child: widget.child,
      ),
    );
  }
}
